package com.ivncorp.evnproject;

import android.app.ListFragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ivncorp.evnproject.adapter.SlidingMenuAdapter;
import com.ivncorp.evnproject.model.SlidingMenuItem;

public class FragmentMenu extends ListFragment {

	private ListView lv;
	private SlidingMenuAdapter adapter;
	private String[] navMenuTitles;
	private TypedArray navMenuIcons;

	// private ListView listview;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.slidingmenu, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		lv = (ListView) getView().findViewById(R.id.list_slidermenu);
		// load slide menu items
		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

		// nav drawer icons from resources
		navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

		adapter = new SlidingMenuAdapter(getActivity());

		// adding nav drawer items to array
		// Home
		for(int i=0;i<navMenuIcons.length();i++)
		{
		adapter.add(new SlidingMenuItem(navMenuTitles[i], navMenuIcons
				.getResourceId(i, -1)));
		}
		lv.setAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView lv, View v, int position, long id) {
		switchFragment(position);
	}

	// the meat of switching the above fragment
	private void switchFragment(int position) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof ActivityMain) {
			ActivityMain fca = (ActivityMain) getActivity();
			fca.switchContent(position);
		}
	}

}
